/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jy.springbootcmsservice.modules.sys.api;

import com.jy.springbootcmsservice.common.base.controller.BaseController;
import com.jy.springbootcmsservice.common.base.entity.BaseResult;
import com.jy.springbootcmsservice.common.constant.HttpStatus;
import com.jy.springbootcmsservice.common.utils.StringUtils;
import com.jy.springbootcmsservice.modules.sys.entity.Menu;
import com.jy.springbootcmsservice.modules.sys.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单Controller
 *
 * @author tm
 * @version 2020-11-23
 */
@RestController
@RequestMapping(value = "${project-config.apiUrl}/${project-config.version}/sys/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @RequiresPermissions("sys:menu:view")
    @RequestMapping(value = "get")
    public BaseResult get(String id, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        if(StringUtils.isNotBlank(id)){
            Menu menu = menuService.get(id);
            result.setData(menu);
        }else {
            result.error(response,
                    "id不能为空");
        }
        return result;
    }

    @RequiresPermissions("sys:menu:view")
    @RequestMapping(value = "list")
    public BaseResult list(Menu menu) {
        List<Menu> menuList = new ArrayList<>();
        List<Menu> sourceList = menuService.findList(menu);
        Menu.sortList(menuList, sourceList, Menu.getRootId(), true);
        return BaseResult.createSuccess(menuList);
    }

    @RequiresPermissions("sys:menu:edit")
    @RequestMapping(value = "save")
    public BaseResult save(Menu menu, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        if (beanValidator(response, result, menu)) {
            menuService.save(menu);
        }
        return result;
    }

    @RequiresPermissions("sys:menu:edit")
    @RequestMapping(value = "delete")
    public BaseResult delete(String id, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        if (StringUtils.isNotBlank(id)) {
            menuService.deleteById(id);
        } else {
            response.setStatus(HttpStatus.STATUS_400);
            result.setError("id不能为空");
        }
        return result;
    }

    /**
     * 获取树形数据
     *
     * @param extId
     * @param isShowHide
     * @param response
     * @return
     */
    @RequestMapping(value = "treeData")
    public BaseResult treeData(Menu menu, @RequestParam(required = false) String extId, @RequestParam(required = false) String isShowHide, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<Menu> list = menuService.findList(menu);
        for (int i = 0; i < list.size(); i++) {
            Menu e = list.get(i);
            // 判断extId是否为e的子菜单
            boolean isNotChild = extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1;
            // 如果extId是空，或者不为空且不为子菜单
            if (StringUtils.isBlank(extId) || isNotChild) {
                if (isShowHide != null && isShowHide.equals("0") && e.getIsShow().equals("0")) {
                    continue;
                }
                Map<String, Object> map = new HashMap<>(16);
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        result.setData(mapList);
        return result;
    }
}
